//
//  mineViewController.m
//  YuErmuYing
//
//  Created by Tiancheng on 16/8/8.
//  Copyright © 2016年 Tiancheng. All rights reserved.
//

#import "mineViewController.h"
#import "TCLYReservationViewController.h"
#import "TCLYHeaderView.h"
#import "TCLYMineModel.h"
#import "TCLYMineTableViewCell.h"
#import "TCLYReservationViewController.h"
#import "TCLYTieZiViewController.h"
#import "TCLYTaskViewController.h"
#import "TCLYSettingViewController.h"
#import "TCLYNewsViewController.h"
#import "TCLYSigninViewController.h"
#import "TCLYWalletViewController.h"
#import "TCLYAboutViewController.h"
#import "Masonry.h"
#import "Macros.h"
#import "UIImageView+WebCache.h"
#import "TCLYUser.h"

@interface mineViewController () <UITableViewDataSource, UITableViewDelegate>

@property (strong, nonatomic) UITableView *mainTableView;
@property (nonatomic, strong) TCLYHeaderView * headerView;
@property (nonatomic, strong) TCLYMineModel * mineModel;
@property (nonatomic, strong) TCLYUser * user;
@end

@implementation mineViewController

- (void)viewDidLoad {
    [super viewDidLoad];
//    NSLog(@"我的tabBar");
    self.navigationController.navigationBarHidden = YES;
    
    UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, screenWidth, 20)];
    view.backgroundColor = navColor;
    [self.view addSubview:view];
//    self.view.backgroundColor = statusColor;
    
    self.user = [[TCLYUser alloc] init];
    //购买成功后要刷新以下数据
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(exchange) name:@"exchange" object:nil];
    //修改资料成功后刷新数据
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(exchange) name:@"loadingMineHome" object:nil];
    //签到成功后要刷新数据
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(signInSucceed) name:@"signInSucceed" object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(createView) name:@"loadMineSucceed" object:nil];
    self.mineModel = [[TCLYMineModel alloc] init];
    NSDictionary * dic = [NSDictionary dictionaryWithObjectsAndKeys:self.user.userId, @"uid", nil];
    [self.mineModel loadMineParameters:dic];
    [self.mineModel loadCellImage];
    [self.mineModel loadCellLabelText];
}
-(void)viewWillAppear:(BOOL)animated{
    self.navigationController.navigationBarHidden = YES;
    self.tabBarController.tabBar.hidden = NO;
}
-(void) createView{
    
    
    self.headerView = [[TCLYHeaderView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 190) IntegralLabelText:self.mineModel.mineDic[@"code"] SignInLabelText:self.mineModel.mineDic[@"past"] CashLabelText:self.mineModel.mineDic[@"money"]];
    
    [self.headerView.newsBtn addTarget:self action:@selector(newsBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    NSURL * url = [NSURL URLWithString:self.mineModel.mineDic[@"pic"]];
    [self.headerView.headerImageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"LYJia2"]];
    self.headerView.nickNameLabel.text = self.mineModel.mineDic[@"name"];
    
    self.mainTableView = [[UITableView alloc] init];
    self.mainTableView.tableHeaderView = self.headerView;
    self.mainTableView.backgroundColor = [UIColor colorWithRed:244/255.0 green:244/255.0 blue:244/255.0 alpha:1];
    [self.mainTableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    self.mainTableView.delegate = self;
    self.mainTableView.dataSource = self;
    [self.view addSubview:self.mainTableView];
    [self.mainTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(20);
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.bottom.mas_equalTo(-45);
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
// tabelView 必须实现的代理
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.mineModel.cellImageArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString * identifier = @"mineCell";
    TCLYMineTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[TCLYMineTableViewCell alloc] initWithReuseIdentifier:identifier];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.cellImageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@",self.mineModel.cellImageArr[indexPath.row]]];
    cell.cellLabel.text = self.mineModel.cellLabelTextArr[indexPath.row];
    return cell;
}
//点击cell触发的代理
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    switch (indexPath.row) {
        case 0:{
            TCLYWalletViewController * walletViewCtr = [[TCLYWalletViewController alloc] init];
            walletViewCtr.moneyStr = self.mineModel.mineDic[@"money"];
            [self.navigationController pushViewController:walletViewCtr animated:YES];
            self.navigationController.navigationBarHidden = NO;
            self.tabBarController.tabBar.hidden = YES;
            NSLog(@"钱包界面");
        }
            break;
        case 1:{
            NSLog(@"预定");
            TCLYReservationViewController * reservationViewCtr = [[TCLYReservationViewController alloc] init];
            [self.navigationController pushViewController:reservationViewCtr animated:YES];
            self.navigationController.navigationBarHidden = NO;
        }
            break;
        case 2:{
            NSLog(@"帖子");
            TCLYTieZiViewController * tieZiViewCtr = [[TCLYTieZiViewController alloc] init];
            [self.navigationController pushViewController:tieZiViewCtr animated:YES];
            self.navigationController.navigationBarHidden = NO;
        }
            break;
        case 3:{
            TCLYTaskViewController * taskViewCtr = [[TCLYTaskViewController alloc] init];
            [self.navigationController pushViewController:taskViewCtr animated:YES];
            NSLog(@"任务");
            self.navigationController.navigationBarHidden = NO;
        }
            break;
        case 4:{
            TCLYSigninViewController * signinViewCtr = [[TCLYSigninViewController alloc] init];
            signinViewCtr.isMineViewCtr = YES;
            [self.navigationController pushViewController:signinViewCtr animated:YES];
            self.navigationController.navigationBarHidden = NO;
            NSLog(@"签到");
        }
            break;
        case 5:{
            TCLYSettingViewController * settingViewCtr = [[TCLYSettingViewController alloc] init];
            [self.navigationController pushViewController:settingViewCtr animated:YES];
            self.navigationController.navigationBarHidden = NO;
            NSLog(@"设置");
        }
            break;
        case 6:{
            TCLYAboutViewController * aboutViewCtr = [[TCLYAboutViewController alloc] init];
            [self.navigationController pushViewController:aboutViewCtr animated:YES];
            self.navigationController.navigationBarHidden = NO;
        }
            break;
    }
}
//消息按钮
-(void)newsBtnAction:(id)sender{
    NSDictionary * dic = [NSDictionary dictionaryWithObjectsAndKeys: self.user.userId, @"uid", nil];
    [self.mineModel disappearNewsPromptParameter:dic];
    
    self.tabBarItem.badgeValue = nil;
    
    TCLYNewsViewController * newsViewCtr = [[TCLYNewsViewController alloc] init];
    [self.navigationController pushViewController:newsViewCtr animated:YES];
    self.navigationController.navigationBarHidden = NO;
}
//改变行的高度（实现主个代理方法后 rowHeight 设定的高度无效）
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}
-(void)signInSucceed{
//    NSLog(@"shoudao");
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(createView) name:@"loadMineSucceed" object:nil];
    NSDictionary * dic = [NSDictionary dictionaryWithObjectsAndKeys:self.user.userId, @"uid", nil];
    [self.mineModel loadMineParameters:dic];
}
-(void)exchange{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(createView) name:@"loadMineSucceed" object:nil];
    NSDictionary * dic = [NSDictionary dictionaryWithObjectsAndKeys:self.user.userId, @"uid", nil];
    [self.mineModel loadMineParameters:dic];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
